package org.aceor.mddal.common.sqlparser.result.condition.value;

import com.google.common.base.Objects;

import java.util.List;

/**
 * 用于sql中?绑定信息存储
 * Created by lxue on 16/3/24.
 */
public class BindingValue implements Value {
    private int index;

    public BindingValue(int index) {
        this.index = index;
    }

    @Override
    public Comparable<?> fillValue(List<Object> args) {
        Object arg = args.get(index);
        if (arg instanceof Comparable<?>) {
            return (Comparable<?>) arg;
        } else {
            throw new IllegalArgumentException();
        }
    }

    @Override
    public int compareTo(Object o) {
        throw new UnsupportedOperationException("只用于传递,不使用");
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("index=",index)
                .toString();
    }

    @Override
    public int hashCode() {
        return index;
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof BindingValue)) {
            return false;
        }
        BindingValue value = (BindingValue) obj;
        return value.index == this.index;
    }
}
